<?php

namespace app\service;

use app\model\StatusModel;
use app\tool\SingletonTrait;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;

class StatusService
{
    use SingletonTrait;

    protected $statusData = [];
    protected array $byId = [];
    protected array $byCode = [];

    /**
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function __construct()
    {
        $this->statusData = (new \app\model\StatusModel)->cache()->select();
        $this->byId = $this->statusData->column(null, 'id');
        $byCodeList = $this->statusData->column(null, 'code');
        $this->byCode = array_map(function ($tmp) {
            $tmp['id'] = intval($tmp['id']);
            return $tmp;
        }, $byCodeList);
    }

    /**
     * 通过 Code 获取状态 ids
     * @param $corresponds
     * @return array
     */
    public function getStatusIdsByCode($corresponds): array
    {
        return StatusModel::where(['code', 'IN', $corresponds])->column("id");
    }


    public function byId(int $statusId, string $column = null)
    {
        if (!empty($column)) {
            return $this->byId[$statusId][$column] ?? null;
        }
        return $this->byId[$statusId] ?? null;
    }

    public function byCode(string $statusCode, string $column = null)
    {
        if (!empty($column)) {
            return $this->byCode[$statusCode][$column] ?? null;
        }
        return $this->byCode[$statusCode] ?? null;
    }

    /**
     * @param array $statusCodeList
     * @param bool $exclude
     * @param string|null $implodeKey
     * @return array|string
     */
    public function byCodeMap(array $statusCodeList, bool $exclude = false, string $implodeKey = null)
    {
        $res = [];
        if (!$exclude) {
            foreach ($statusCodeList as $code) {
                if (isset($this->byCode[$code])) {
                    $res[$code] = $this->byCode[$code];
                }
            }
        } else {
            foreach ($this->byCode as $code => $item) {
                if (!in_array($code, $statusCodeList)) {
                    $res[$code] = $item;
                }
            }
        }
        if (!empty($implodeKey)) {
            return implode(',', array_column($res, $implodeKey));
        }
        return $res;
    }

    /**
     * 获取未完成的状态
     * @return array
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function getNotStartedStatus(): array
    {
        $config = OptionsService::getInstance()->getOptionsConfigMultiItemData('default_task_status', ["default_status", "default_ready_status", "default_wait_assemble_status"]);
        if (empty($config)) {
            return [];
        }
        $res = [];
        foreach ($config as $configItem) {
            $res[] = $this->byCode[$configItem];
        }
        return $res;
    }
}
